package org.jmc.refmanager.domain.dao.jpa;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.jmc.refmanager.domain.dao.ChangeDao;
import org.jmc.refmanager.domain.dao.QueryParam;
import org.jmc.refmanager.domain.model.Change;
import org.jmc.refmanager.domain.model.Change.Operation;
import org.jmc.refmanager.domain.model.Change.TaskStatus;
import org.springframework.stereotype.Repository;


@Repository("taskDao")
public class ChangeDaoImpl extends GenericDaoJpa<Change, Integer> implements
	ChangeDao {

	public ChangeDaoImpl() {
		super(Change.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Change> findGroupTasks(Integer directionId) {
		List<TaskStatus> inStatus = new ArrayList<TaskStatus>();;
		inStatus.add(TaskStatus.AVAILABLE);
		inStatus.add(TaskStatus.RESERVED);
		
		return super.findByNamedQuery(Change.FIND_GROUP_TASKS, directionId, inStatus);
	}

	@Override
	public List<Change> findMyGroupTasks(QueryParam<Change> params, String matricule, List<TaskStatus> inStatus, List<Operation> inOperation) {
//		if (inStatus == null || inStatus.size() == 0) {
//			Query query = super.createNamedQuery(Change.FIND_MY_GROUP_TASKS);
//			return super.findAndPaging(params, query, matricule);
//		} else {
			Query query = super.createNamedQuery(Change.FIND_MY_GROUP_TASKS_IN_STATUS_FOR_OP);
			return super.findAndPaging(params, query, matricule, inStatus, inOperation);
//		}
	}

	@Override
	public Integer countMyGroupTasks(String matricule, List<TaskStatus> inStatus, List<Operation> inOperation) {
//		if (inStatus == null || inStatus.size() == 0) {
//			Query query = super.createNamedQuery(Change.COUNT_MY_GROUP_TASKS);
//			return super.count(query, matricule);
//		} else {
			Query query = super.createNamedQuery(Change.COUNT_MY_GROUP_TASKS_IN_STATUS_FOR_OP);
			return super.count(query, matricule, inStatus, inOperation);
//		}
	}

	@Override
	public List<Change> findMyTasks(QueryParam<Change> params, String matricule) {
		Query query = super.createNamedQuery(Change.FIND_MY_TASKS);
		return super.findAndPaging(params, query, matricule);
	}

	@Override
	public Integer countMyTasks(String matricule) {
		Query query = super.createNamedQuery(Change.COUNT_MY_TASKS);
		return super.count(query, matricule);
	}

}
